package mumethod;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import mudbconnection.DBPhysicals;
import mudbconnection.DBQuestionnaires;
import muobject.Physical;
import muobject.Questionnaire;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("AccessQuestionnairesService")
public class AccessQuestionnairesService implements AccessQuestionnaires{
	
	@Autowired
	private DBQuestionnaires dbQuestionnaires;
	private List<Questionnaire> questionnaires;
	private List<String> testDates;
	private Questionnaire questionnaire;
	private ValidFunction validFunction;
	
	@SuppressWarnings("rawtypes")
	private List rows;
	private DateControl dateControl;
	
	public String getTestDate(String stno, String year, String month, int frequency) {
		dateControl = new LibFunction();
		return dateControl.convertDate(dbQuestionnaires.getTestDate(stno, year, month, frequency));
	}
	
	public List getTestDateByYearStno(String stno, String year) {
		return dbQuestionnaires.getTestDateByYearStno(stno, year);
	}
	
	public List getTestDateByYearMonthStno(String stno, String year, String month) {
		return dbQuestionnaires.getTestDateByYearMonthStno(stno, year, month);
	}
	
	public String getAnswer(String stno, String year, String month, String testDate, String qno) {
		dateControl = new LibFunction();
		return dateControl.EmptyString(dbQuestionnaires.getAnswer(stno, year, month, testDate, qno));
	}
	
	public List getListAnswer(String stno, String year, String month, String qno){
		return dbQuestionnaires.getListAnswer(stno, year, month, qno);
	}
	
	public List getListAnswerByYearTestdateStno(String stno, String year, String testDate){
		return dbQuestionnaires.getListAnswerByYearTestdateStno(stno, year, testDate);
	}
	
	public boolean existQuestionnaire(String stno, String year, String month, String testDate, String qno) {
		return dbQuestionnaires.getAnswer(stno, year, month, testDate, qno)!= null;
	}
	
	public boolean setQuestionnaireFromTable(Table table, String yeararg, String testDatearg, String montharg, String stnoarg){
		String stno, qno, answer, years, month, block; 
		int frequency;
		Date testDate;
		try {
			for (TableItem item : table.getItems()) {
				int i = 1;
					if(!testDatearg.equals("")){
						block = "false";
						testDate = Date.valueOf(testDatearg);
						stno = stnoarg;
						years = yeararg;
						qno = item.getText(0);
						answer = item.getText(i);
						month = montharg;
						frequency = i;
						if(!answer.equals("0") & !answer.equals(""))
							questionnaire = new Questionnaire(0, stno, years, month, frequency, testDate, qno, answer, block, null, null);
						else
							questionnaire = new Questionnaire(0, stno, years, month, frequency, testDate, qno, "0", block, null, null);
						
						if(!existQuestionnaire(stno, years, month, testDatearg, qno)){
							dbQuestionnaires.setQuestionnaire(questionnaire);
						}else{
							dbQuestionnaires.updateQuestionnaire(questionnaire);
						}
					i++;
				}
				
			}
		}catch(Exception e){
			return false;
		}
		return true;
	}
	
	public boolean setQuestionnaire(Questionnaire questionnaire){
		return dbQuestionnaires.setQuestionnaire(questionnaire);
	}
	
	public boolean updateQuestionnaire(Questionnaire questionnaire){
		return dbQuestionnaires.updateQuestionnaire(questionnaire);
	}
	
	public boolean deleteQuestionnaireByTestDatearg(Table table, String yeararg, String testDatearg, String montharg, String stnoarg){
		String stno, qno, years, month, block; 
		try {
			for (TableItem item : table.getItems()) {
				block = "false";
				stno = stnoarg;
				years = yeararg;
				qno = item.getText(0);
				month = montharg;
				if(existQuestionnaire(stno, years, month, testDatearg, qno)){
					dbQuestionnaires.deleteQuestionnaire(stno, years, month, qno, testDatearg);
				}
			}
		}catch(Exception e){
			return false;
		}
		return true;
	}
	
//	public Questionnaire getQuestionnaireByYearMonthStno(String stno, String year, String month, String testDate){
//		validFunction = new ValidFunction();
//	}
	
}
